package com.weilive.api.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.weilive.api.ResourceManager;
import com.weilive.api.dao.mapper.ResourceMapper;
import com.weilive.api.dao.model.AdvanceVideo;
import com.weilive.api.dao.model.BannerAds;
import com.weilive.api.dao.model.MainView;
import com.weilive.api.dao.model.PaymentType;
import com.weilive.api.dao.model.VjLevel;
import com.weilive.api.dao.model.VjSubType;
import com.weilive.api.dao.model.VjType;

/**
 * 资源管理类
 * 
 * @author houzhiwei
 *
 */
@Service
public class ResourceSystem {
	private static final Logger logger = LoggerFactory.getLogger(MainViewSystem.class);

	private static final int PAGE_TYPE_MAIN = 1;// 首页
	private static final int PAGE_TYPE_DISCOVER = 2;// 发现
	@Autowired
	private ResourceMapper resourceDao;
	// 资源
	private List<AdvanceVideo> advanceConfigList;// 预告资源
	private List<BannerAds> bannerAdsConfigList;// 广告资源
	private Map<Integer, List<MainView>> mainViewConfigMap;// 推荐资源
	private Map<Integer, List<MainView>> discoverConfigMap;// 发现资源

	// ---------------------db---------------------
	private List<VjType> vjTypeConfigList;
	private Map<Integer, VjType> vjTypeConfigMap;
	private Map<Integer, VjSubType> vjSubTypeConfigMap;
	private List<VjLevel> vjLevelConfigList;
	private Map<Integer, PaymentType> paymentTypeConfigMap;

	@PostConstruct
	public void init() {
		loadData();
		loadStaticData();
		ResourceManager.register("reloadData", this.getClass(), this);
	}

	private void loadStaticData() {
		List<VjSubType> vjSubTypeList = resourceDao.getAllVjSubTypes();
		// 排序
		Collections.sort(vjSubTypeList, new Comparator<VjSubType>() {
			@Override
			public int compare(VjSubType o1, VjSubType o2) {
				if (o1.getVjTypeId() < o2.getVjTypeId()) {
					return -1;
				} else if (o1.getVjTypeId() == o2.getVjTypeId()) {
					return o1.getVjSubTypeId() - o2.getVjSubTypeId();
				} else {
					return 1;
				}
			}
		});
		Map<Integer, VjSubType> vjSubTypeConfigMap = new HashMap<Integer, VjSubType>();

		Map<Integer, List<VjSubType>> subList = new HashMap<Integer, List<VjSubType>>();

		for (VjSubType v : vjSubTypeList) {
			vjSubTypeConfigMap.put(v.getVjSubTypeId(), v);

			List<VjSubType> sub = subList.get(v.getVjTypeId());
			if (sub == null) {
				sub = new ArrayList<VjSubType>();
				subList.put(v.getVjTypeId(), sub);
			}
			sub.add(v);
		}

		List<VjType> vjTypeList = resourceDao.getAllVjTypes();
		Map<Integer, VjType> vjTypeMap = new HashMap<Integer, VjType>();
		for (VjType v : vjTypeList) {
			List<VjSubType> sub = subList.get(v.getVjTypeId());
			v.setVjSubTypeList(sub == null ? new ArrayList<VjSubType>() : sub);
			vjTypeMap.put(v.getVjTypeId(), v);
		}
		// 排序
		Collections.sort(vjTypeList, new Comparator<VjType>() {
			@Override
			public int compare(VjType o1, VjType o2) {
				return o1.getSort() - o2.getSort();
			}
		});

		List<VjLevel> vjlvList = resourceDao.getAllVjLevels();
		// 排序
		Collections.sort(vjlvList, new Comparator<VjLevel>() {
			@Override
			public int compare(VjLevel o1, VjLevel o2) {
				return o1.getLevel() - o2.getLevel();
			}
		});

		List<PaymentType> payList = resourceDao.getAllPaymentTypes();
		Map<Integer, PaymentType> oaymentTypeMap = new HashMap<Integer, PaymentType>();
		for (PaymentType type : payList) {
			oaymentTypeMap.put(type.getRoomPaymentTypeId(), type);
		}

		this.setVjLevelConfigList(vjlvList);
		this.setPaymentTypeConfigMap(oaymentTypeMap);
		this.setVjTypeConfigList(vjTypeList);
		this.setVjTypeConfigMap(vjTypeMap);
		this.setVjSubTypeConfigMap(vjSubTypeConfigMap);
	}

	private void loadData() {

		List<AdvanceVideo> advanceList = resourceDao.getAllAdvanceVideo();
		// 排序
		Collections.sort(advanceList, new Comparator<AdvanceVideo>() {
			@Override
			public int compare(AdvanceVideo o1, AdvanceVideo o2) {
				return (int) (o1.getStartTime().getTime() - o2.getStartTime().getTime());
			}
		});

		List<BannerAds> bannerAdsList = resourceDao.getAllBannerAds();
		// 排序
		Collections.sort(bannerAdsList, new Comparator<BannerAds>() {
			@Override
			public int compare(BannerAds o1, BannerAds o2) {
				return o1.getSort() - o2.getSort();
			}
		});
		// 推荐
		List<MainView> allList = resourceDao.getAllViews();

		List<MainView> mainViewList = new ArrayList<MainView>();

		List<MainView> discoverList = new ArrayList<MainView>();

		for (MainView v : allList) {
			// 小咖不需要
			if (v.getVjTypeId() == 0) {
				continue;
			}
			if (v.getPageType() == PAGE_TYPE_MAIN) {
				mainViewList.add(v);
			} else if (v.getPageType() == PAGE_TYPE_DISCOVER) {
				discoverList.add(v);
			}
		}
		// 排序
		Collections.sort(mainViewList, new Comparator<MainView>() {
			@Override
			public int compare(MainView o1, MainView o2) {
				if (o1.getVjTypeId() < o2.getVjTypeId()) {
					return -1;
				} else if (o1.getVjTypeId() == o2.getVjTypeId()) {
					return o1.getSort() - o2.getSort();
				} else {
					return 1;
				}
			}
		});

		// 排序
		Collections.sort(discoverList, new Comparator<MainView>() {
			@Override
			public int compare(MainView o1, MainView o2) {
				if (o1.getVjTypeId() < o2.getVjTypeId()) {
					return -1;
				} else if (o1.getVjTypeId() == o2.getVjTypeId()) {
					return o1.getSort() - o2.getSort();
				} else {
					return 1;
				}
			}
		});
		Map<Integer, List<MainView>> discoverConfigMap = new HashMap<Integer, List<MainView>>();
		Map<Integer, List<MainView>> mainViewConfigMap = new HashMap<Integer, List<MainView>>();

		for (MainView view : mainViewList) {
			List<MainView> views = mainViewConfigMap.get(view.getVjTypeId());
			if (views == null) {
				views = new ArrayList<MainView>();
				mainViewConfigMap.put(view.getVjTypeId(), views);
			}
			views.add(view);
		}

		for (MainView view : discoverList) {

			List<MainView> views = discoverConfigMap.get(view.getVjTypeId());
			if (views == null) {
				views = new ArrayList<MainView>();
				discoverConfigMap.put(view.getVjTypeId(), views);
			}
			views.add(view);
		}

		this.setAdvanceConfigList(advanceList);
		this.setBannerAdsConfigList(bannerAdsList);
		this.setMainViewConfigMap(mainViewConfigMap);
		this.setDiscoverConfigMap(discoverConfigMap);
	}

	public void reloadData() {
		long begin = System.currentTimeMillis();
		loadData();
		logger.info("-----------重新加载资源成功-----------!耗时={}ms", (System.currentTimeMillis() - begin));
	}

	public int getPayment(int type) {
		PaymentType pay = this.getPaymentTypeConfigMap().get(type);
		if (pay == null) {
			return 0;
		}
		return pay.getPayment();
	}

	public Map<Integer, PaymentType> getPaymentTypeConfigMap() {
		return paymentTypeConfigMap;
	}

	public void setPaymentTypeConfigMap(Map<Integer, PaymentType> paymentTypeConfigMap) {
		this.paymentTypeConfigMap = paymentTypeConfigMap;
	}

	public List<VjLevel> getVjLevelConfigList() {
		return vjLevelConfigList;
	}

	public void setVjLevelConfigList(List<VjLevel> vjLevelConfigList) {
		this.vjLevelConfigList = vjLevelConfigList;
	}

	public Map<Integer, VjSubType> getVjSubTypeConfigMap() {
		return vjSubTypeConfigMap;
	}

	public void setVjSubTypeConfigMap(Map<Integer, VjSubType> vjSubTypeConfigMap) {
		this.vjSubTypeConfigMap = vjSubTypeConfigMap;
	}

	public Map<Integer, VjType> getVjTypeConfigMap() {
		return vjTypeConfigMap;
	}

	public void setVjTypeConfigMap(Map<Integer, VjType> vjTypeConfigMap) {
		this.vjTypeConfigMap = vjTypeConfigMap;
	}

	public List<AdvanceVideo> getAdvanceConfigList() {
		return advanceConfigList;
	}

	public void setAdvanceConfigList(List<AdvanceVideo> advanceConfigList) {
		this.advanceConfigList = advanceConfigList;
	}

	public List<BannerAds> getBannerAdsConfigList() {
		return bannerAdsConfigList;
	}

	public void setBannerAdsConfigList(List<BannerAds> bannerAdsConfigList) {
		this.bannerAdsConfigList = bannerAdsConfigList;
	}

	public Map<Integer, List<MainView>> getMainViewConfigMap() {
		return mainViewConfigMap;
	}

	public void setMainViewConfigMap(Map<Integer, List<MainView>> mainViewConfigMap) {
		this.mainViewConfigMap = mainViewConfigMap;
	}

	public Map<Integer, List<MainView>> getDiscoverConfigMap() {
		return discoverConfigMap;
	}

	public void setDiscoverConfigMap(Map<Integer, List<MainView>> discoverConfigMap) {
		this.discoverConfigMap = discoverConfigMap;
	}

	public List<VjType> getVjTypeConfigList() {
		return vjTypeConfigList;
	}

	public void setVjTypeConfigList(List<VjType> vjTypeConfigList) {
		this.vjTypeConfigList = vjTypeConfigList;
	}

	public VjType getVjType(int typeId) {
		return getVjTypeConfigMap().get(typeId);
	}

	public List<MainView> getMainViewList(int vjTypeId) {
		if (getMainViewConfigMap().containsKey(vjTypeId)) {
			return getMainViewConfigMap().get(vjTypeId);
		}
		return new ArrayList<MainView>();
	}

	public List<MainView> getDiscoverViewList(int vjTypeId) {
		if (getDiscoverConfigMap().containsKey(vjTypeId)) {
			return getDiscoverConfigMap().get(vjTypeId);
		}
		return new ArrayList<MainView>();
	}
}
